package com.coocaa.x.service.liteplayer;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaService;
import com.coocaa.x.framework.utils.AsyncTask;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayer;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerClientService;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerListener;
import com.coocaa.x.service.liteplayer.aidl.ILitePlayerMonitor;
import com.coocaa.x.service.liteplayer.aidl.ReadyInfo;
import com.coocaa.x.service.liteplayer.aidl.VideoSourceInfo;

import java.util.Vector;

/**
 * Created by lu on 15-7-29.
 */
public class LitePlayerClientServiceStub extends ILitePlayerClientService.Stub {
    private static final String tag = "clientservice";

    private class MyPlayer extends ILitePlayer.Stub {

        @Override
        public void errorOccurred(String arg0, String arg1) throws RemoteException {
            XServiceManager.getLitePlayerKernelProxyService().getPlayer().errorOccurred(arg0, arg1);
        }

        @Override
        public int getCurrentTimeInMillis() throws RemoteException {
            int time = XServiceManager.getLitePlayerKernelProxyService().getPlayer().getCurrentTimeInMillis();
            return time;
        }

        @Override
        public int getDuration() throws RemoteException {
            int time = XServiceManager.getLitePlayerKernelProxyService().getPlayer().getDuration();
            return time;
        }

        @Override
        public void getReady(ReadyInfo arg0) throws RemoteException {
            LogUtils.d(tag, "getReady  name:" + arg0);
            XServiceManager.getLitePlayerKernelProxyService().getPlayer().getReady(arg0);
        }

        @Override
        public boolean isPlaying() throws RemoteException {
            return XServiceManager.getLitePlayerKernelProxyService().getPlayer().isPlaying();
        }

        @Override
        public boolean pause() throws RemoteException {
            return XServiceManager.getLitePlayerKernelProxyService().getPlayer().pause();
        }

        @Override
        public boolean resume() throws RemoteException {
            return XServiceManager.getLitePlayerKernelProxyService().getPlayer().resume();
        }

        @Override
        public boolean seekTo(int arg0) throws RemoteException {
            return XServiceManager.getLitePlayerKernelProxyService().getPlayer().seekTo(arg0);
        }

        @Override
        public boolean start(VideoSourceInfo arg0, String arg1) throws RemoteException {
            return XServiceManager.getLitePlayerKernelProxyService().getPlayer().start(arg0, arg1);
        }

        @Override
        public boolean stop() throws RemoteException {
            return XServiceManager.getLitePlayerKernelProxyService().getPlayer().stop();
        }
    }

    private class MyListener extends ILitePlayerListener.Stub {

        @Override
        public void onBufferingUpdate(final int arg0) throws RemoteException {
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onBufferingUpdate(arg0);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }

        }

        @Override
        public void onCompletion() throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onCompletion();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public boolean onError(final int arg0, final int arg1) throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onError(arg0, arg1);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
            return false;
        }

        @Override
        public boolean onInfo(final int arg0, final int arg1) throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onInfo(arg0, arg1);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
            return false;
        }

        @Override
        public void onPlayPaused() throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayPaused();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public void onPlayResumed() throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayResumed();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public void onPlayStarted(final String arg0, final int arg1) throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayStarted(arg0, arg1);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public void onPlayStopped() throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPlayStopped();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public void onPrepared() throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onPrepared();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public void onProgressChanged(final int arg0) throws RemoteException {
            // TODO Auto-generated method stub
            synchronized (clientListeners) {
                for (final CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                    AsyncTask.runOnThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                i.i.onProgressChanged(arg0);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }

    }

    private class MyMonitor extends ILitePlayerMonitor.Stub {

        @Override
        public void addPlayerListener(ILitePlayerListener arg0) throws RemoteException {
            synchronized (clientListeners) {
                if (arg0 != null) {
                    IBinder binder = arg0.asBinder();
                    CoocaaService.AIDLInterface<ILitePlayerListener> i = new CoocaaService.AIDLInterface<ILitePlayerListener>(
                            binder, arg0);
                    clientListeners.add(i);
                }
            }
        }

        @Override
        public void removePlayerListener(ILitePlayerListener arg0) throws RemoteException {
            synchronized (clientListeners) {
                if (arg0 != null) {
                    IBinder binder = arg0.asBinder();
                    for (CoocaaService.AIDLInterface<ILitePlayerListener> i : clientListeners) {
                        if (i.binder == binder) {
                            clientListeners.remove(i);
                            break;
                        }
                    }
                }
            }
        }

    }

    private Vector<CoocaaService.AIDLInterface<ILitePlayerListener>> clientListeners = new Vector<CoocaaService.AIDLInterface<ILitePlayerListener>>();

    private MyPlayer myPlayer = new MyPlayer();
    private MyMonitor myMonitor = new MyMonitor();
    private MyListener myListener = new MyListener();
    private Context mContext = null;

    public LitePlayerClientServiceStub(Context context) {
        mContext = context;
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                try {
                    LogUtils.d("CoocaaService", " XServiceManager.getLitePlayerKernelProxyService() is null :" + (XServiceManager.getLitePlayerKernelProxyService() == null));
                    XServiceManager.getLitePlayerKernelProxyService().getPlayerMonitor()
                            .addPlayerListener(myListener);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    public void whoAmI() {
        LogUtils.d("wai", "<<LitePlayerClientService>> who am i??  " + mContext.getPackageName());
    }

    @Override
    public ILitePlayer getPlayer() throws RemoteException {
        return myPlayer;
    }

    @Override
    public ILitePlayerMonitor getPlayerMonitor() throws RemoteException {
        return myMonitor;
    }
}
